<?php
/**
 * Class that operate on table 'hoteles'. Database Mysql.
 *
 * @author: http://phpdao.com
 * @date: 2013-11-23 21:12
 */
class HotelesMySqlDAO implements HotelesDAO{

	/**
	 * Get Domain object by primry key
	 *
	 * @param String $id primary key
	 * @return HotelesMySql 
	 */
	public function load($id){
		$sql = 'SELECT * FROM hoteles WHERE id_hotel = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->setNumber($id);
		return $this->getRow($sqlQuery);
	}

	/**
	 * Get all records from table
	 */
	public function queryAll(){
		$sql = 'SELECT * FROM hoteles';
		$sqlQuery = new SqlQuery($sql);
		return $this->getList($sqlQuery);
	}
	
	/**
	 * Get all records from table ordered by field
	 *
	 * @param $orderColumn column name
	 */
	public function queryAllOrderBy($orderColumn){
		$sql = 'SELECT * FROM hoteles ORDER BY '.$orderColumn;
		$sqlQuery = new SqlQuery($sql);
		return $this->getList($sqlQuery);
	}
	
	/**
 	 * Delete record from table
 	 * @param hotele primary key
 	 */
	public function delete($id_hotel){
		$sql = 'DELETE FROM hoteles WHERE id_hotel = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->setNumber($id_hotel);
		return $this->executeUpdate($sqlQuery);
	}
	
	/**
 	 * Insert record to table
 	 *
 	 * @param HotelesMySql hotele
 	 */
	public function insert($hotele){
		$sql = 'INSERT INTO hoteles (nombre, direccion, telefono, email, pagina_web, descripcion_cotizacion, estado_hotel) VALUES (?, ?, ?, ?, ?, ?, ?)';
		$sqlQuery = new SqlQuery($sql);
		
		$sqlQuery->set($hotele->nombre);
		$sqlQuery->set($hotele->direccion);
		$sqlQuery->set($hotele->telefono);
		$sqlQuery->set($hotele->email);
		$sqlQuery->set($hotele->paginaWeb);
		$sqlQuery->set($hotele->descripcionCotizacion);
		$sqlQuery->set($hotele->estadoHotel);

		$id = $this->executeInsert($sqlQuery);	
		$hotele->idHotel = $id;
		return $id;
	}
	
	/**
 	 * Update record in table
 	 *
 	 * @param HotelesMySql hotele
 	 */
	public function update($hotele){
		$sql = 'UPDATE hoteles SET nombre = ?, direccion = ?, telefono = ?, email = ?, pagina_web = ?, descripcion_cotizacion = ?, estado_hotel = ? WHERE id_hotel = ?';
		$sqlQuery = new SqlQuery($sql);
		
		$sqlQuery->set($hotele->nombre);
		$sqlQuery->set($hotele->direccion);
		$sqlQuery->set($hotele->telefono);
		$sqlQuery->set($hotele->email);
		$sqlQuery->set($hotele->paginaWeb);
		$sqlQuery->set($hotele->descripcionCotizacion);
		$sqlQuery->set($hotele->estadoHotel);

		$sqlQuery->setNumber($hotele->idHotel);
		return $this->executeUpdate($sqlQuery);
	}

	/**
 	 * Delete all rows
 	 */
	public function clean(){
		$sql = 'DELETE FROM hoteles';
		$sqlQuery = new SqlQuery($sql);
		return $this->executeUpdate($sqlQuery);
	}

	public function queryByNombre($value){
		$sql = 'SELECT * FROM hoteles WHERE nombre = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->set($value);
		return $this->getList($sqlQuery);
	}

	public function queryByDireccion($value){
		$sql = 'SELECT * FROM hoteles WHERE direccion = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->set($value);
		return $this->getList($sqlQuery);
	}

	public function queryByTelefono($value){
		$sql = 'SELECT * FROM hoteles WHERE telefono = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->set($value);
		return $this->getList($sqlQuery);
	}

	public function queryByEmail($value){
		$sql = 'SELECT * FROM hoteles WHERE email = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->set($value);
		return $this->getList($sqlQuery);
	}

	public function queryByPaginaWeb($value){
		$sql = 'SELECT * FROM hoteles WHERE pagina_web = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->set($value);
		return $this->getList($sqlQuery);
	}

	public function queryByDescripcionCotizacion($value){
		$sql = 'SELECT * FROM hoteles WHERE descripcion_cotizacion = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->set($value);
		return $this->getList($sqlQuery);
	}

	public function queryByEstadoHotel($value){
		$sql = 'SELECT * FROM hoteles WHERE estado_hotel = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->set($value);
		return $this->getList($sqlQuery);
	}


	public function deleteByNombre($value){
		$sql = 'DELETE FROM hoteles WHERE nombre = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->set($value);
		return $this->executeUpdate($sqlQuery);
	}

	public function deleteByDireccion($value){
		$sql = 'DELETE FROM hoteles WHERE direccion = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->set($value);
		return $this->executeUpdate($sqlQuery);
	}

	public function deleteByTelefono($value){
		$sql = 'DELETE FROM hoteles WHERE telefono = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->set($value);
		return $this->executeUpdate($sqlQuery);
	}

	public function deleteByEmail($value){
		$sql = 'DELETE FROM hoteles WHERE email = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->set($value);
		return $this->executeUpdate($sqlQuery);
	}

	public function deleteByPaginaWeb($value){
		$sql = 'DELETE FROM hoteles WHERE pagina_web = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->set($value);
		return $this->executeUpdate($sqlQuery);
	}

	public function deleteByDescripcionCotizacion($value){
		$sql = 'DELETE FROM hoteles WHERE descripcion_cotizacion = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->set($value);
		return $this->executeUpdate($sqlQuery);
	}

	public function deleteByEstadoHotel($value){
		$sql = 'DELETE FROM hoteles WHERE estado_hotel = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->set($value);
		return $this->executeUpdate($sqlQuery);
	}


	
	/**
	 * Read row
	 *
	 * @return HotelesMySql 
	 */
	protected function readRow($row){
		$hotele = new Hotele();
		
		$hotele->idHotel = $row['id_hotel'];
		$hotele->nombre = $row['nombre'];
		$hotele->direccion = $row['direccion'];
		$hotele->telefono = $row['telefono'];
		$hotele->email = $row['email'];
		$hotele->paginaWeb = $row['pagina_web'];
		$hotele->descripcionCotizacion = $row['descripcion_cotizacion'];
		$hotele->estadoHotel = $row['estado_hotel'];

		return $hotele;
	}
	
	protected function getList($sqlQuery){
		$tab = QueryExecutor::execute($sqlQuery);
		$ret = array();
		for($i=0;$i<count($tab);$i++){
			$ret[$i] = $this->readRow($tab[$i]);
		}
		return $ret;
	}
	
	/**
	 * Get row
	 *
	 * @return HotelesMySql 
	 */
	protected function getRow($sqlQuery){
		$tab = QueryExecutor::execute($sqlQuery);
		if(count($tab)==0){
			return null;
		}
		return $this->readRow($tab[0]);		
	}
	
	/**
	 * Execute sql query
	 */
	protected function execute($sqlQuery){
		return QueryExecutor::execute($sqlQuery);
	}
	
		
	/**
	 * Execute sql query
	 */
	protected function executeUpdate($sqlQuery){
		return QueryExecutor::executeUpdate($sqlQuery);
	}

	/**
	 * Query for one row and one column
	 */
	protected function querySingleResult($sqlQuery){
		return QueryExecutor::queryForString($sqlQuery);
	}

	/**
	 * Insert row to table
	 */
	protected function executeInsert($sqlQuery){
		return QueryExecutor::executeInsert($sqlQuery);
	}
}
?>